//
//  WanfaManager.m
//  SSC
//
//  Created by 红沙尘 on 17/6/17.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "WanfaManager.h"

@implementation WanfaManager
{
    NSMutableArray *_wanfaArray;
    NSMutableArray *nameArray;
    NSMutableArray *descArray;
    NSMutableArray *maxArray;
}

- (id)init {
    
    self = [super init];
    
    if (self) {
        
        _wanfaArray = [[NSMutableArray alloc] init];
        nameArray   = [[NSMutableArray alloc] init];
        descArray   = [[NSMutableArray alloc] init];
        maxArray    = [[NSMutableArray alloc] init];
        
        NSMutableArray *idArrayM = [[NSMutableArray alloc] init];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Wanfa" ofType:@"plist"];
        
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        
        for (NSDictionary *dict in array) {
            
            [nameArray addObject:dict[@"name"]];
            [descArray addObject:dict[@"desc"]];
            [maxArray  addObject:dict[@"max"]];
            [idArrayM  addObject:dict[@"playid"]];
            
        }
        
        
        
        for (int i = 0; i < nameArray.count; i ++) {
            WanfaModel *model = [[WanfaModel alloc] initWithNamed:nameArray[i]
                                                             desc:descArray[i]
                                                         maxPrice:maxArray[i]];
            model.playid = [idArrayM[i] intValue];
            [_wanfaArray addObject:model];
            
            //TODO:这块逻辑需要优化，playid的赋值操作应该在WanfaModel的初始化中
            if ([model.name rangeOfString:@"前三组六"].length > 0) {
                model.playid = 11;
            } else if ([model.name rangeOfString:@"前三组三"].length > 0) {
                model.playid = 12;
            } else if ([model.name rangeOfString:@"中三组六"].length > 0) {
                model.playid = 13;
            } else if ([model.name rangeOfString:@"中三组三"].length > 0) {
                model.playid = 14;
            }
        }
    }
    
    return self;
}

- (WanfaModel *)wanfaModelAtIndex:(NSInteger)index  {
    return _wanfaArray[index];
}

- (NSMutableArray *)dataOfWanfaAtIndex:(NSInteger)index {
    WanfaModel *model = _wanfaArray[index];
    return model.dataArray;
}

- (NSArray *)wanfaNamesArray {
    return nameArray;
}

- (NSArray *)wanfaShuomingArray {
    return descArray;
}

- (void)bettEndtime {
    [SSCRequest bettEndtimeSuccess:^(NSDictionary *responseObject) {
        
    } fail:^(NSError *error) {
        
    }];
}

//追号的时候会以追号的倍数为准，所以有一个倍数的参数
+ (NSMutableArray *)haomaWithModel:(WanfaModel *)wanfaModel beishu:(NSString *)beishu{

    NSMutableArray *list = @[].mutableCopy;
    
    for (int i = 0; i < wanfaModel.dataArray.count; i ++) {
        
        HaomaSelectModel *selectModel = wanfaModel.dataArray[i];
        
        NSMutableArray<HaomaModel *> *array = [selectModel allHaoma];
        NSString *code = @"";
        
        for (int j = 0; j < array.count; j ++) {

            HaomaModel *haomaModel = array[j];
            
            if (haomaModel.selected) {
                code = [code stringByAppendingFormat:@"%d",haomaModel.number];
            }
        }
        
        if (code.length > 0) {
            
            if (beishu.length == 0) {
                beishu = [NSString stringWithFormat:@"%d",wanfaModel.beishu];
            }
            
            NSInteger zhushu = wanfaModel.zhushu;
            
            if ([wanfaModel.name rangeOfString:@"一星定位胆"].length > 0) {
                zhushu = code.length;
            }
            
            NSString *name = [wanfaModel.name stringByReplacingOccurrencesOfString:@"▼" withString:@""];
            
            NSMutableDictionary *dict = @{ @"playid":[NSString stringWithFormat:@"%d",wanfaModel.playid],
                                           @"amount":[NSString stringWithFormat:@"%lu",2*[beishu intValue]*zhushu],
                                           @"code":code,
                                           @"doubles":beishu,
                                           @"numbers":[NSString stringWithFormat:@"%lu",zhushu],
                                           @"playname":name}.mutableCopy;
            
            if ([wanfaModel.name rangeOfString:@"一星定位胆"].length > 0) {
                dict[@"place"] = selectModel.weiId;
            }
            
            [list addObject:dict];
        }
    }
    
    return list;
}



@end
